package com.eedi.framework.org.controller.sys;

import com.eedi.framework.org.controller.sys.vo.*;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.org.controller.sys.vo.*;
import com.eedi.framework.org.service.SysOrgService;
import com.eedi.framework.security.core.util.SecurityFrameworkUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "系统界面 - 机构管理")
@RestController
@RequestMapping("/sys-org")
@Validated
@Slf4j
public class SysOrgController {
    @Resource
    SysOrgService sysOrgService;


    @GetMapping("/page")
    @Operation(summary = "机构列表")
    @PreAuthorize("@sys.hasPermission('sys:org:query')")
    public CommonResult<PageResult<SysOrgInfoResp>> query(SysOrgPageReq req) {
        return success(sysOrgService.page(req));
    }

    @GetMapping("/details")
    @Operation(summary = "机构详情")
    @Parameter(name = "orgInfoId", description = "机构编号", required = true)
    @PreAuthorize("@sys.hasPermission('sys:org:get')")
    public CommonResult<SysOrgInfoDetailsResp> get(@RequestParam("orgInfoId") String orgInfoId) {
        return success(sysOrgService.get(orgInfoId));
    }


    @PostMapping ("/create")
    @Operation(summary = "机构创建")
    @PreAuthorize("@sys.hasPermission('sys:org:create')")
    public CommonResult<SysOrgCreateResp> create(@Valid @RequestBody SysOrgCreateReq req) {
        return success(sysOrgService.create(req, SecurityFrameworkUtils.getTenantId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机构")
    @PreAuthorize("@sys.hasPermission('sys:org:enable')")
    public CommonResult<Boolean> enable(@Valid @RequestBody SysOrgUpReq req) {
        sysOrgService.updateOrgInfo(req);
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码")
    @Parameter(name = "orgUserId", description = "用户编号", required = true)
    @PreAuthorize("@sys.hasPermission('sys:org:rest-password')")
    public CommonResult<String> resetPassword(@RequestParam("orgUserId") String orgUserId) {
        String defualtPassword = sysOrgService.resetPassword(orgUserId);
        return success(defualtPassword);
    }
    @GetMapping("/count")
    @Operation(summary = "机构数量")
    @PreAuthorize("@sys.hasPermission('sys:org:count')")
    public CommonResult<Long> countOrgInfo() {
        return success(sysOrgService.countSysOrgInfo());
    }
}
